package cn.echoedu.dao;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import cn.echoedu.daoconstants.Databases;
import cn.echoedu.util.L;

public class ConnectionClass {
	public static final Databases DATABASE_CODE=Databases.MySql;
	private DataSource ds;
	public ConnectionClass(){
		Context ctx;
		try {
			ctx = new InitialContext();
		
		switch(DATABASE_CODE){
		case MySql:
	  	this.ds=(DataSource)ctx.lookup("java:comp/env/jdbc/myschool");
			break;
		default:


		}
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public  Connection getConnection(){
		Connection con = null;
		try {
			con = ds.getConnection();
			con.setAutoCommit(false);
			} catch (SQLException e) {
			// TODO Auto-generated catch block
			L.p("mysql getConnection is null");
			e.printStackTrace();
		}
		L.p("mysql getConnection"+con);
		
		return con;
	}
	public void rollBackAndClose(Connection con)
	{
		try {
			con.rollback();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public void commitAndClose(Connection con){
		try {
			con.commit();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
